package com.miss.spjyx

import android.content.Context
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import com.miss.spjyx.model.Danju
import com.miss.spjyx.network.CommonCallback
import com.miss.spjyx.network.Requester
import com.miss.spjyx.widget.ProgressDialog

/**
 * 入库单fragment
 *
 * @author wl
 * @since 2019/08/11 17:09
 */
class RkdFragment : Fragment() {
    lateinit var adapter: DanjuAdapter

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment_rkd, container, false)

        val rv_data = view.findViewById<RecyclerView>(R.id.rv_data)
        adapter = DanjuAdapter(activity!!, true)
        rv_data.adapter = adapter

        return view
    }

    override fun onResume() {
        super.onResume()
        loadData()
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (!hidden) {
            loadData()
        }
    }

    private fun loadData() {
        val sp = CkApp.app.getSharedPreferences("spj", Context.MODE_PRIVATE)
        val warehouse = sp.getString("set_ck", "")
        val rkdtype = sp.getString("set_rkd", "")
        Requester.apiService()
            .getCrkd(warehouse, rkdtype)
            .enqueue(object : CommonCallback<List<Danju>>(activity!!) {
                override fun onData(data: List<Danju>?) {
                    if (data.isNullOrEmpty()) {
                        if (activity != null) {
                            Toast.makeText(activity, "无数据", Toast.LENGTH_SHORT).show()
                        }
                    }
                    adapter.data = data
                }
            })
    }
}